﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UFIDA.U9.Cust.XH.PlugBE.Tool;
using UFSoft.UBF.Business;
using UFSoft.UBF.Eventing;
namespace UFIDA.U9.Cust.XH.PlugBE
{
    /// <summary>
    /// 科目保存事件
    /// </summary>
    public class AccountBasicSaveExtend : IEventSubscriber
    {
        public void Notify(params object[] args)
        {
            #region 从事件参数中取得当前业务实体													 

            if (args == null || args.Length == 0 || !(args[0] is UFSoft.UBF.Business.EntityEvent))
                return;
            BusinessEntity.EntityKey key = ((UFSoft.UBF.Business.EntityEvent)args[0]).EntityKey;
            if (key == null)
                return;
            //	标准科目账簿属性数据       
            UFIDA.U9.CBO.FI.NaturalAccount.NaturalAccountSOBProperty entity = key.GetEntity() as UFIDA.U9.CBO.FI.NaturalAccount.NaturalAccountSOBProperty;
            if (entity == null)
                return;
            #endregion
            if (PubWebClass.IsSyncToOA())
            {
                dataModel model = new dataModel();
                Dictionary<string, string> opeartion = new Dictionary<string, string>();
                opeartion.Add("operationDate", DateTime.Now.ToString("yyyy-MM-dd"));
                opeartion.Add("operationTime", DateTime.Now.ToString("HH:mm:ss"));
                opeartion.Add("operator", "1");
                model.operationinfo = opeartion;
                OAAccountModel o = new OAAccountModel(entity);
                model.mainTable = o;
                List<dataModel> data = new List<dataModel>();
                data.Add(model);
                string res = PubWebClass.RecordHttp("同步科目", OAUrlHelper.getAccountjUrl, data);
                if (res.Contains("异常信息"))
                {
                    throw new Exception(res);
                }
                else
                {
                    JObject jobj = JObject.Parse(res);
                    if (jobj["status"]?.ToString() != "1")
                    {
                        JObject d = JObject.Parse(jobj["datajson"].ToString());
                        JArray ary = JArray.Parse(d["faildata"].ToString());
                        string msg = "";
                        foreach (dynamic item in ary)
                        {
                            msg += item["mainTable"];
                        }
                        if (!string.IsNullOrEmpty(msg))
                        {
                            throw new Exception("同步OA失败" + msg);
                        }

                    }
                }
            }



        }

        private class dataModel
        {
            public dynamic operationinfo { get; set; }
            public OAAccountModel mainTable { get; set; }
        }
        public class OAAccountModel
        {
            public OAAccountModel(UFIDA.U9.CBO.FI.NaturalAccount.NaturalAccountSOBProperty m)
            {
                //this.id = m.ID.ToString();
                this.kmbm = m.NaturalAccount.Code;
                this.kmmc = m.NaturalAccount.Name;
                this.kmfx = m.NaturalAccount.BalanceDirection.Value.ToString();
                if (m.NaturalAccountSOBSegmentUseRoles.Count > 0)
                {
                    CBO.FI.NaturalAccount.NaturalAccountSOBSegmentUseRole o = m.NaturalAccountSOBSegmentUseRoles[0];
                    this.sfsjyxfzhs = o.Segment9 ? "0" : "1";
                    this.sfsjryfzhs = o.Segment3 ? "0" : "1";
                    this.sfsjkhfzhs = o.Segment4 ? "0" : "1";
                    this.sfsjgysfzhs = o.Segment5 ? "0" : "1";
                    this.sfsjxmfzhs = o.Segment6 ? "0" : "1";
                }

                if (this.kmbm.Length > 4)
                {
                    this.sjkmbm = this.kmbm.Substring(0, this.kmbm.Length - 2);
                }

            }


            ///// <summary>
            ///// id
            ///// </summary>
            //public string id { get; set; }

            /// <summary>
            /// 科目编码
            /// </summary>
            public string kmbm { get; set; }

            /// <summary>
            /// 科目名称
            /// </summary>
            public string kmmc { get; set; }

            /// <summary>
            /// 科目方向枚举：[0:借方,1:贷方]
            /// </summary>
            public string kmfx { get; set; }

            /// <summary>
            /// 是否涉及银行辅助核算枚举：[0:是,1:否]
            /// </summary>
            public string sfsjyxfzhs { get { return string.IsNullOrEmpty(this._sfsjyxfzhs) ? "1" : this._sfsjyxfzhs; } set { this._sfsjyxfzhs = value; } }

            private string _sfsjyxfzhs;

            /// <summary>
            /// 是否涉及人员辅助核算枚举：[0:是,1:否]
            /// </summary>
            public string sfsjryfzhs { get { return string.IsNullOrEmpty(this._sfsjryfzhs) ? "1" : this._sfsjryfzhs; } set { this._sfsjryfzhs = value; } }

            private string _sfsjryfzhs;
            /// <summary>
            /// 是否涉及客户辅助核算枚举：[0:是,1:否]
            /// </summary>
            public string sfsjkhfzhs { get { return string.IsNullOrEmpty(this._sfsjkhfzhs) ? "1" : this._sfsjkhfzhs; } set { this._sfsjkhfzhs = value; } }

            private string _sfsjkhfzhs;
            /// <summary>
            /// 是否涉及供应商辅助核算枚举：[0:是,1:否]
            /// </summary>
            public string sfsjgysfzhs { get { return string.IsNullOrEmpty(this._sfsjgysfzhs) ? "1" : this._sfsjgysfzhs; } set { this._sfsjgysfzhs = value; } }

            private string _sfsjgysfzhs;

            /// <summary>
            /// 是否涉及项目辅助核算枚举：[0:是,1:否]
            /// </summary>
            public string sfsjxmfzhs { get { return string.IsNullOrEmpty(this._sfsjxmfzhs) ? "1" : this._sfsjxmfzhs; } set { this._sfsjxmfzhs = value; } }

            private string _sfsjxmfzhs;

            /// <summary>
            /// 上级科目编码传入：浏览框的数据标题
            /// </summary>
            public string sjkmbm { get; set; }

            ///// <summary>
            ///// 上级科目名称
            ///// </summary>
            //public string sjkmmc { get; set; }

            ///// <summary>
            ///// 往来科目编码传入：浏览框的数据标题
            ///// </summary>
            //public string wlkmbm { get; set; }

            ///// <summary>
            ///// 往来科目名称
            ///// </summary>
            //public string wlkmmc { get; set; }

            ///// <summary>
            ///// 贷方现金流程编码
            ///// </summary>
            //public string dfxjlcbm { get; set; }

            ///// <summary>
            ///// 借方现金流程编码
            ///// </summary>
            //public string jfxjlcbm { get; set; }
        }
    }
}
